package com.raos.example.shardingjdbc2basic;

import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

/**
 * Sharding-Jdbc分库分表实战之数据源工具类
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2021/8/22 21:13
 */
public class DataSourceUtil {

    private static final String HOST = "192.168.235.110";

    private static final int PORT = 3306;

    private static final String USER_NAME = "root";

    private static final String PASSWORD = "Mysql@123";

    /**
     * 创建数据连接源
     * @param dataSourceName
     * @return
     */
    public static DataSource createDataSource(final String dataSourceName) {
        HikariDataSource result = new HikariDataSource();
        result.setDriverClassName("com.mysql.jdbc.Driver");
        result.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8",
                HOST, PORT, dataSourceName));
        result.setUsername(USER_NAME);
        result.setPassword(PASSWORD);
        return result;
    }

}
